﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DataLib.Common;
using DataLib.Accounts.Bus;
using System.Collections.Generic;

namespace Ewts.BaseData
{
    public partial class CustomerModify : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int id = (Convert.ToInt32(Request.Params["id"]));
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    DataTable dt = new DataTable();
                    switch (curUser.UserType)
                    {
                        case "AD":
                            dt = curUser.GetUsersByType("", null, "业务员");
                            break;
                        //case "AA":
                        //    dt = curUser.GetUsersByType("AA", curUser.CompanyCity, curUser.UserName);
                        //    break;
                        //case "AS":
                        //    dt = curUser.GetUsersByType("", curUser.CompanyCity, curUser.UserName);
                        //    break;
                        //case "DD":
                        //case "AR":
                        //    dt = curUser.GetUsersByType("", curUser.CompanyCity, "业务员");
                        //    break;
                        default:
                            dt = curUser.GetUsersByType("", curUser.CompanyCity, "业务员");
                            break;
                    }
                    //if (dt != null && dt.Rows.Count > 0)
                    //{
                    //    dropSaleMan.DataSource = dt;
                    //    dropSaleMan.DataTextField = "TrueName";
                    //    dropSaleMan.DataValueField = "UserName";
                    //    dropSaleMan.DataBind();
                    //}
                    //dropSaleMan.Items.Insert(0, new ListItem("请选择业务员"));
                    //if (curUser.UserType != "AD" && curUser.UserType != "AR" && curUser.UserType != "DD")
                    //{
                    //    dropSaleMan.SelectedValue = curUser.UserName;
                    //    //dropSaleMan.Enabled = false;
                    //}
                    ////dropSaleMan.Items.Insert(0, new ListItem("请选择业务员"));
                    //if (curUser.UserType != "AD" && curUser.UserType != "AR" && curUser.UserType != "DD")
                    //{
                    //    //dropSaleMan.SelectedValue = curUser.UserName;
                    //    dropSaleMan.Enabled = false;
                    //}

                    ShowInfo(id);
                }
            }
        }

        private void ShowInfo(int id)
        {
            hidcumid.Value = id.ToString();
            DataLib.BLL.Cumtomer bll = new DataLib.BLL.Cumtomer();
            DataLib.Model.Cumtomer model = bll.GetModel(id);
            
            this.txtcompanyname.Text = model.companyname;
            this.txtshortname.Text = model.shortname;
            this.txtcustomercode.Value = model.customercode;
            this.txtcity.Text = model.city;
            this.txtsettlement.Text = model.settlement;
            this.txtpostcode.Text = model.postcode;
            this.txtlinkman.Text = model.linkman;
            this.txtlinkman2.Text = model.linkman2;
            this.txtphone.Text = model.phone;
            this.txtphone2.Text = model.phone2;
            this.txtEmail.Text = model.Email;
            this.txtEmail2.Text = model.Email2;
            this.txtBankName.Text = model.BankName;
            this.txtBankAccount.Text = model.BankAccount;
            this.txtBankCode.Text = model.BankCode;
            this.txtPutOnRecord.Text = model.PutOnRecord;
            this.txtBankAddress.Text = model.BankAddress;
            txtState.Text = model.State;
            txtProvince.Text = model.province;
            //dropSaleMan.SelectedValue = model.SaleMan;
        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = ForEstimat();
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            else if (errorcode.InnerHtml != "")
            {
                MessageBox.Show(this, errorcode.InnerHtml);
                return;
            }
            int id = (Convert.ToInt32(Request.Params["id"]));
            string companyname = this.txtcompanyname.Text;
            string shortname = this.txtshortname.Text;
            string customercode = this.txtcustomercode.Value;
            string city = this.txtcity.Text;
            string settlement = this.txtsettlement.Text;
            string postcode = this.txtpostcode.Text;
            string linkman = this.txtlinkman.Text;
            string linkman2 = this.txtlinkman2.Text;
            string phone = this.txtphone.Text;
            string phone2 = this.txtphone2.Text;
            string Email = this.txtEmail.Text;
            string Email2 = this.txtEmail2.Text;
            string BankName = this.txtBankName.Text;
            string BankAccount = this.txtBankAccount.Text;
            string BankCode = this.txtBankCode.Text;
            string PutOnRecord = this.txtPutOnRecord.Text;
            string BankAddress = this.txtBankAddress.Text;


            DataLib.Model.Cumtomer model = new DataLib.Model.Cumtomer();
            model.id = id;
            model.companyname = companyname;
            model.shortname = shortname;
            model.customercode = customercode;
            model.city = city;
            model.settlement = settlement;
            model.postcode = postcode;
            model.linkman = linkman;
            model.linkman2 = linkman2;
            model.phone = phone;
            model.phone2 = phone2;
            model.Email = Email;
            model.Email2 = Email2; 
            model.BankName = BankName;
            model.BankAccount = BankAccount;
            model.BankCode = BankCode;
            model.PutOnRecord = PutOnRecord;
            model.BankAddress = BankAddress;
            //model.SaleMan = dropSaleMan.SelectedValue;
            model.State = txtState.Text;
            model.province = txtProvince.Text;
            DataLib.BLL.Cumtomer bll = new DataLib.BLL.Cumtomer();
            bll.Update(model);
            //客户地址更新
            List<DataLib.Model.CumtomerAddress> cumlist = new DataLib.BLL.CumtomerAddress().GetModelList(" customerid=" + id);
            if (cumlist.Count>0 &&cumlist!=null)
            {
                DataLib.Model.CumtomerAddress cum = cumlist[0];
                cum.companyname = companyname;
                new DataLib.BLL.CumtomerAddress().Update(cum);
            }

            DataLib.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "CustomerList.aspx");

        }

        /// <summary>
        /// 判断输入的内容
        /// </summary>
        /// <returns>错误信息</returns>
        protected string ForEstimat()
        {
            string strErr = "";
            if (txtcompanyname.Text.Trim().Length == 0)
            {
                strErr += "客户名称不能为空！\\n";
            }
            if (txtcustomercode.Value.Trim().Length == 0)
            {
                strErr += "客户编码不能为空！\\n";
            }
 

            return strErr;
        }

        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomerList.aspx");
        }
    }
}
